/*
 * Copyright (C) 2025 ameise <ameise.wang@gmail.com> - All Rights Reserved
 *
 * This file is part of e3ds.
 *
 * e3ds is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * e3ds is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with e3ds. If not, see <https://www.gnu.org/licenses/>.
 */

package queue

import (
	"gitee.com/ameise84/e3ds/deque"
	"sync"
)

type IQueue[T any] interface {
	Enqueue(T) error
	EnqueueBatch([]T) error
	Peek() (T, error) //查看队列头元素
	Dequeue() (T, error)
	DequeueBatch(int) ([]T, error)
	DequeueHalf() ([]T, error)
	DequeueAll() ([]T, error)
	IsEmpty() bool
	Size() int
}

func NewQueue[T any](cap int) IQueue[T] {
	if cap < 0 {
		panic("deque.NewDeque: cap must be >= 0")
	}
	if cap == 0 {
		return deque.NewList[T]()
	}
	return deque.NewRing[T](cap)
}

func NewQueueLock[T any](mu sync.Locker, cap int) IQueue[T] {
	if cap < 0 {
		panic("deque.NewDeque: cap must be >= 0")
	}
	if cap == 0 {
		return deque.NewListLock[T](mu)
	}
	return deque.NewRingLock[T](mu, cap)
}
